//
//  YYPublishController.swift
//  SinaWeibo
//
//  Created by Apple on 16/5/10.
//  Copyright © 2016年 devshaw. All rights reserved.
//

import UIKit


let holder = "请输入你想要说的话"


class YYPublishController: UIViewController,UITextViewDelegate{

    override func viewDidLoad() {
        super.viewDidLoad()

        setupNav()

    }

    private func setupNav(){
        //添加navigationItem 的左边按钮
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回", style: UIBarButtonItemStyle.Plain, target: self, action: "dismiss")
        // navgationitem 右边按钮
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "发布", style: UIBarButtonItemStyle.Plain, target: self, action: "publish")
        //隐藏右边按钮
        navigationItem.rightBarButtonItem?.enabled = false

        //初始化 label 实例化
        let label = UILabel(frame: CGRectMake(0,0,200,44))
        //创建字符串
        let str = "发布微博\n 换行了吗"
        //换行
        label.numberOfLines = 0
        //让文字居中显示
        label.textAlignment = .Center
        //需要修改字体的大小和颜色
        let attribute =  NSMutableAttributedString(string: str)

        attribute.addAttributes([NSFontAttributeName:UIFont.systemFontOfSize(16),NSForegroundColorAttributeName:UIColor.blackColor()], range: NSMakeRange(0,4))

        attribute.addAttributes([NSFontAttributeName:UIFont.systemFontOfSize(13),NSForegroundColorAttributeName:UIColor.orangeColor()], range: NSMakeRange(5, str.characters.count - 5))

        //把文字属性更改赋值给 label 的attributedText
        label.attributedText = attribute

        //把 label 作为 navgationitem的 titleview
        navigationItem.titleView = label


    }


    private func setupTextView(){
        view.addSubview(textView)

        textView.font = UIFont.systemFontOfSize(16)

        textView.holdSting = holder

        textView.delegate = self

        textView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(view.snp_edges)
        }
    }

    @objc private func publish(){


    }

    @objc private func dismiss(){
        dismissViewControllerAnimated(true) { () -> Void in
            //
        }
    }


    private lazy var textView: YYTextView = YYTextView()
}


extension YYPublishController{

    func textViewDidChange(textView: UITextView) {

        //判断长度
        if textView.hasText(){
            self.textView.holdSting = ""

        }else{

            self.textView.holdSting = holder
        }

        // 重绘
        self.textView.setNeedsDisplay()

        navigationItem.rightBarButtonItem?.enabled = true
    }
}
